<?php
/**
 * Created By kefu_virus_com
 * Author: Virus
 * Date: 2020/2/8
 * Time: 17:34
 */

namespace app\common\response;


use think\facade\Request;
use think\Response;
use app\common\response\Jump;

class Common extends Response
{
    protected $type = "html";

    public function __construct()
    {
        parent::__construct();
        // 判断是否是ajax提交数据
        if (Request::isAjax()) {
            $this->type = 'json';
        }
    }

    /**
     * @param string $info
     * @param int $status
     */
    public function initialize($info = '', $status = 0)
    {
        if ($this->type === 'json') {
            header('Content-type:application/json; charset=utf-8');
            exit(
            json_encode(
                [
                    'status' => $status,
                    'info'   => $info,
                ]
            )
            );
        } elseif ($this->type === 'html') {
            if ($status === 0) {
                // 失败
                app(Jump::class)->error($info);
            } else {
                // 成功
                app(Jump::class)->success($info);
            }
        }
    }
}